package com.jsgt.greekfire.ui.trading.otc

import android.text.TextUtils
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager.widget.ViewPager
import com.fhx.core.base.BaseFragment
import com.fhx.core.goToActivity
import com.fhx.core.utils.ToastUtils
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.jsgt.greekfire.IS_ACTIVATION
import com.jsgt.greekfire.LOGIN_STATUS
import com.jsgt.greekfire.R
import com.jsgt.greekfire.factory.OTCTraddingModelFactory
import com.jsgt.greekfire.ui.home.entrance.EntranceActivity
import com.jsgt.greekfire.ui.mainassets.adapter.TabEntity
import com.jsgt.greekfire.ui.trading.adapter.PagerAdapter
import com.jsgt.greekfire.viewmodel.OTCTraddingViewModel
import kotlinx.android.synthetic.main.fragment_otc_trading_sell.*
import org.jetbrains.anko.startActivity

class OTCTradingSellFragment : BaseFragment() {
    private var mTabEntities =
        ArrayList<CustomTabEntity>()

    private val mFragments: MutableList<BaseFragment> = ArrayList()
    var coinCode: String? = null
    private val otcTraddingViewModel: OTCTraddingViewModel by lazy {
        ViewModelProvider(
            this,
            OTCTraddingModelFactory()
        ).get(OTCTraddingViewModel::class.java)
    }

    override fun getLayoutId(): Int = R.layout.fragment_otc_trading_sell

    override fun main() {
        val tabArray = java.util.ArrayList<String>()

        otcTraddingViewModel.getOTCCoinData()
        otcTraddingViewModel.coinlistData.observe(this, Observer {
            if (it != null) {
                var list = it.result
                if (list != null && list.size > 0) {
                    for (str in list) {
                        tabArray.add(str.coinCode!!)
                    }
                    val tabs = java.util.ArrayList<String>()
                    for (i in tabArray.indices) {
                        mFragments.add(OTCTradingSellListFragment.newInstance(tabArray[i])) //全部
                        mTabEntities.add(TabEntity(tabArray[i]))
                        tabs.add(tabArray[i])
                        coinCode = tabArray[0]
                    }
                    viewPager.adapter = PagerAdapter(
                        childFragmentManager,
                        mFragments,
                        tabs
                    )
                    tl_2()
                }
            }
        })
        otcTraddingViewModel.errorLiveData.observe(this, Observer {
            ToastUtils.show(it.toString())
        })
        ivAdd.setOnClickListener {
            //showToast("添加出售广告")
            if (LOGIN_STATUS) {
                if (IS_ACTIVATION == 1) {
                    ToastUtils.show(context!!, getString(R.string.account_no_activation))
                } else {
                    if (!TextUtils.isEmpty(coinCode)) {
                        context!!.startActivity<OTCSellCommissionActivity>(
                            "coinCode" to coinCode
                        )
                    }
                }
            } else {
                goToActivity<EntranceActivity>()
            }
        }

    }

    private fun tl_2() {
        commonTablOTCSell.setTabData(mTabEntities)
        commonTablOTCSell.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                viewPager.setCurrentItem(position)

            }

            override fun onTabReselect(position: Int) {}
        })
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                commonTablOTCSell.setCurrentTab(position)
                coinCode = mTabEntities.get(position).tabTitle
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        viewPager.setCurrentItem(0)
    }

}